
#include "Emitter.h"

#include <ctime>
#include <string>
#include <iostream>
using namespace std;
#include "../SGD Wrappers/SGD_Math.h"
#include <sstream>
#include "Game.h"

Emitter::Emitter(void)
{
	m_pTM = nullptr;
	imageID = -1;

	//filepath = "BFly.png";
	filepath = " ";
	posY				= 0;
	posX				= 0;
	height				= 0;
	width				= 0;
	emitterShape		= RECTANGLE;
	spread				= 0;
	spawnRate		= 0;
	direction			= 0.0f;
	AlphaStart		= 0;
	RcolorStart		= 0;
	GcolorStart		= 0;
	BcolorStart		= 0;
	AlphaEnd			= 0;
	RcolorEnd		= 0;
	GcolorEnd		= 0;
	BcolorEnd		= 0;
	scaleStart			= 0.0f;
	scaleEnd			= 0.0f;
	velocityYStart	= 0.0f;
	velocityXStart	= 0.0f;
	velocityYEnd	= 0.0f; 
	velocityXEnd	= 0.0f;
	lifeMin				= 0.0f;
	lifeMax				= 1.0f;
	rotationStart		= 0.0f;
	rotationRate		= 1.0f;
	maxParticles		= 1;
	looping				= true;
}

Emitter::~Emitter(void)
{

}

void Emitter::Init()
{
	m_pTM = CSGD_TextureManager::GetInstance();

	ostringstream oss;
	oss << "Resources/Graphics/" << filepath.c_str();
	imageID = m_pTM->LoadTexture(oss.str().c_str());

	for(int i = 0; i < maxParticles; i++)
	{
		Particle tbd;

		switch(emitterShape)
				{
				case RECTANGLE:
					{
						tbd.currPosX			= posX + (rand() % width);
						tbd.currPosY			= posY + (rand() % height);
					}
					break;
				case CIRCLE:
					{
						float randomA = float(rand()%100);
						 float randomB = float(rand()%100);
						 randomA = randomA/100.0f;
						 randomB = randomB/100.0f;

						 float radius = height;

						tbd.currPosX			= randomA*radius*cos(2*D3DX_PI*randomB/randomA) + posX;
						tbd.currPosY			= randomA*radius*sin(2*D3DX_PI*randomB/randomA) + posY;
					}
					break;
				case LINE:
					{
						tbd.currPosX			= posX + (rand() % width);
						tbd.currPosY			= posY + (tbd.currPosX * height/ width);
					}
					break;
				}

		tbd.currAlpha		= AlphaStart;
		tbd.currRColor		= RcolorStart;
		tbd.currGColor		= GcolorStart;
		tbd.currBColor		= BcolorStart;

		int tempMaxLife	= lifeMax * 100;
		int tempMinLife		= lifeMin * 100;
		tbd.totalLife			= ((rand() % (tempMaxLife - tempMinLife)) + tempMinLife) / 100.0f;
		tbd.currLife			= 0;

		tbd.currRotation	= rotationStart;
		tbd.currScale		= scaleStart;
		tbd.currVelY			= velocityYStart;
		tbd.currVelX			= velocityXStart;

		if(dead.size() < maxParticles - spawnRate)
			dead.push_back(tbd);
		else
			live.push_back(tbd);
	}
}

void Emitter::Update(float fElapsedTime)
{
	/*if(fElapsedTime == 1 && dead.size() != 0)
	{
		for( int i = 0; i > spawnRate; i++)
		{*/
			if(dead.size() != 0)
			{
				Particle temp;
				temp.currAlpha			= dead[0].currAlpha;
				temp.currBColor		= dead[0].currBColor;
				temp.currGColor		= dead[0].currGColor;
				temp.totalLife			= dead[0].totalLife;
				temp.currLife			= 0.0f;
				temp.currPosX			= dead[0].currPosX;
				temp.currPosY			= dead[0].currPosY;
				temp.currRColor		= dead[0].currRColor;
				temp.currRotation		= dead[0].currRotation;
				temp.currScale			= dead[0].currScale;
				temp.currVelX			= dead[0].currVelX;
				temp.currVelY			= dead[0].currVelY;

				dead.erase(dead.begin());
				live.push_back(temp);
			}
		/*	else
				break;
		}
	}*/
	for(unsigned int i = 0; i < live.size(); i++)
	{
		live[i].currPosY			+= live[i].currVelY * fElapsedTime;
		live[i].currPosX			+= live[i].currVelX * fElapsedTime;
		float percent = live[i].currLife / live[i].totalLife;
		live[i].currAlpha		= Lerp(AlphaStart, AlphaEnd, percent);
		live[i].currRColor		= Lerp(RcolorStart, RcolorEnd, percent);
		live[i].currGColor		= Lerp(GcolorStart, GcolorEnd,percent);
		live[i].currBColor		= Lerp(BcolorStart, BcolorEnd, percent);
		live[i].currScale			= Lerp(scaleStart, scaleEnd, percent);
		live[i].currRotation	= live[i].currRotation + rotationRate * fElapsedTime;
		live[i].currVelX			= Lerp(velocityXStart, velocityXEnd, percent); 
		live[i].currVelY			= Lerp(velocityYStart, velocityYEnd, percent); 

		live[i].currLife += fElapsedTime;

		if(live[i].currLife >= live[i].totalLife)
		{
			live.erase(live.begin() + i);
			if(looping)
			{
				Particle temp;
				temp.currAlpha			= AlphaStart;
				temp.currBColor		= BcolorStart;
				temp.currGColor		= GcolorStart;
				temp.currRColor		= RcolorStart;

				int tempMaxLife		= lifeMax * 100;
				int tempMinLife			= lifeMin * 100;
				temp.currLife			= ((rand() % (tempMaxLife - tempMinLife)) + tempMinLife) / 100.0f;

				switch(emitterShape)
				{
				case RECTANGLE:
					{
						temp.currPosX			= posX + (rand() % width);
						temp.currPosY			= posY + (rand() % height);
					}
					break;
				case CIRCLE:
					{
						float randomA = float(rand()%100);
						 float randomB = float(rand()%100);
						 randomA = randomA/100.0f;
						 randomB = randomB/100.0f;

						 float radius = height;

						temp.currPosX			= randomA*radius*cos(2*D3DX_PI*randomB/randomA) + posX;
						temp.currPosY			= randomA*radius*sin(2*D3DX_PI*randomB/randomA) + posY;
					}
					break;
				case LINE:
					{
						temp.currPosX			= posX + (rand() % width);
						temp.currPosY			= posY + (temp.currPosX * height / width);
					}
					break;
				}
				
				temp.currRotation		= rotationStart;
				temp.currScale			= scaleStart;
				temp.currVelX			= velocityXStart;
				temp.currVelY			= velocityYStart;

				dead.push_back(temp);
			}
		}
	}
}

void Emitter::Render()
{
	for( int i = 0; i < live.size(); i++)
	{
		m_pTM->Draw(imageID, live[i].currPosX, live[i].currPosY, live[i].currScale, live[i].currScale, nullptr, 16.0f, 16.0f, live[i].currRotation,
			D3DCOLOR_ARGB(live[i].currAlpha, live[i].currRColor, live[i].currGColor, live[i].currBColor));
	}
}

void Emitter::Exit()
{
	live.clear();
	dead.clear();

	m_pTM->UnloadTexture(imageID);
	m_pTM->ShutdownTextureManager();
	m_pTM	= nullptr;
	imageID = -1;
}


